package com.shanghaiyimu.yimiyangche.fragment;


import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.BiaoZhunPopAdapter;
import com.shanghaiyimu.yimiyangche.adapter.DianPuPTRAdapter;
import com.shanghaiyimu.yimiyangche.bean.DianPuBean;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class DianpuFragment extends Fragment {

    public DianpuFragment(){

    }

    private TextView text;
    private ListView popListView;
    private PullToRefreshListView pullToRefreshListView;
    private DianPuPTRAdapter adapter;
    private List<DianPuBean> datas = new ArrayList<>();
    private PopupWindow popupWindow;
    private List<String> data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_dianpu, container, false);

        initView(view);
        initData();

        return view;
    }


    private void initData() {
        DianPuBean dianPuBean = null;
        for (int i = 0; i < 15; i++) {
            dianPuBean = new DianPuBean();
            dianPuBean.setName("宜牧养车"+i);
            dianPuBean.setAddress("兴庆公园北门"+i);
            datas.add(dianPuBean);
        }
        adapter.notifyDataSetChanged();
    }

    private void initView(View view) {
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.dianpi_ptr_lv);
        adapter = new DianPuPTRAdapter(getContext(),datas);
        pullToRefreshListView.setAdapter(adapter);

        text = (TextView) view.findViewById(R.id.dianpu_tv);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow!=null&&popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else {
                    showWindow(text);
                }
            }
        });

    }
    private void showWindow(View parent) {
        if (popupWindow == null) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.pop_view, null);
            popListView = (ListView) view.findViewById(R.id.pop_view_lv);
            data = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                data.add("西安市" + i);
            }
            BiaoZhunPopAdapter adapter = new BiaoZhunPopAdapter(data, getContext());
            popListView.setAdapter(adapter);
            popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, 800);
        }
        // 使其聚集,防止无法关闭popupWindow
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
//        ColorDrawable colorDrawable = new ColorDrawable(0x36555654);
//        popupWindow.setBackgroundDrawable(colorDrawable); //设置背景显示图片

        /**
         * 在某个控件的下方展示
         * anchor： 	指定控件
         * x,y: 		偏移量
         */
        popupWindow.showAsDropDown(parent,0,0);
        popListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                text.setText(data.get(i));
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
            }
        });
    }


}
